package com.nebula.core.util;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author zhangtao
 * @create 2024/1/14 14:26
 */


public class DateDerializerCustom extends StdDeserializer<Date> {
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DateDerializerCustom() {
        super(Date.class);
    }

    @Override
    public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonToken currentToken = p.getCurrentToken();
        if (currentToken == JsonToken.VALUE_STRING) {
            return DateUtil.parse(p.getText());
        } else if (currentToken == JsonToken.VALUE_NUMBER_INT) {
            return DateUtil.date(p.getLongValue() * 1000);
        }
        return null;
    }
}

